package sjtu.ist.warehouse4ws.model;

import java.util.ArrayList;
import java.util.List;



/**
 * Epcunits entity. @author MyEclipse Persistence Tools
 */

public class Epcunits  implements java.io.Serializable {


    // Fields    

     private String id;
     private String name;
     private String fileId;
     private String groupId;
     private String versionId;

     private List<Epcunit> epcUnits=new ArrayList<Epcunit>();
 	
     private String uid;
 	
     public List<Epcunit> getEpcUnits() {
 	    return epcUnits;
     }
     public void setEpcUnits(List<Epcunit> epcUnits) {
 	    this.epcUnits = epcUnits;
     }
    // Constructors

    /** default constructor */
    public Epcunits() {
    }

	/** minimal constructor */
    public Epcunits(String id) {
        this.id = id;
    }
    
    /** full constructor */
    public Epcunits(String id, String name, String fileId,String uid) {
        this.id = id;
        this.name = name;
        this.fileId = fileId;
        this.uid=uid;
    }

   
    // Property accessors

    public String getId() {
        return this.id;
    }
    
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public String getFileId() {
        return this.fileId;
    }
    
    public void setFileId(String fileId) {
        this.fileId = fileId;
    }
   

    public Epcunit findSourceDataById(int id){
	    for (Epcunit sourceData:epcUnits){
		    if (sourceData.getId()==id)return sourceData;
	    }
	    return null;
    }
    public void bulidRelationship(int source, int target){
        Epcunit sourceData=this.findSourceDataById(source);
        if(sourceData==null || sourceData.getNext()==null){
        	return;
        }
        sourceData.getNext().add(target);
        	

    }
    public void removeRelationship(Epcunit source, Epcunit target){
    	source.getNext().remove(source.getNext().indexOf(target.getId()));
    }
    public int findMaxId(){
    	int max=0;
    	for (Epcunit e:epcUnits){
    		if (max<e.getId())
    			max=e.getId();
    	}
    	return max;
    }
    public List<Epcunit> findNextFunction(int id,List<Epcunit> taskList){
	    Epcunit element=new Epcunit();
	    element=this.findSourceDataById(id);
	
		for (int i:element.getNext()){
			element=this.findSourceDataById(i);
			if (element.getType()=="function"
				|| element.getType()=="connector"
			|| element.getType()=="end") taskList.add(element);
			else findNextFunction(i,taskList);
		}
	
	    return taskList;
    }
    public List<Epcunit> findSource(Epcunit target){
    	List<Epcunit> sourceList=new ArrayList<Epcunit>();
    	for (Epcunit e:epcUnits){
    		for(int i:e.getNext()){
    			Epcunit next=this.findSourceDataById(i);
    			if (next==target) sourceList.add(e);
    			
    		}
    	}
    	return sourceList;
    }
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}
	public String getGroupId() {
		return groupId;
	}
	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}
	public String getVersionId() {
		return versionId;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
    





}